package com.renjibo.sharkerwave.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.renjibo.sharkerwave.R;
import com.renjibo.sharkerwave.models.data.CourseData;
import com.renjibo.sharkerwave.ui.activity.FreeAuditionActivity;
import com.renjibo.sharkerwave.ui.activity.GreatestHitsActivity;
import com.renjibo.sharkerwave.ui.activity.PlayVideoActivity;
import com.renjibo.sharkerwave.ui.view.DividerItemDecoration;
import com.zhy.autolayout.AutoRelativeLayout;

/**
 * 学习页面的适配器
 * 任继波
 * Created by Administrator on 2017/4/8.
 */

public class HomeAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private final LayoutInflater inflater;
    private final int ONE = 0;
    private final int TWO = 1;
    private final int THREE = 2;
    private final int ITEMCOUNT = 3;
    private Context context;
    private CourseData result = new CourseData();

    public HomeAdapter(Context context, CourseData result) {
        this.context = context;
        inflater = LayoutInflater.from(context);
        this.result = result;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = null;
        RecyclerView.ViewHolder viewHolder = null;
        switch (viewType) {
            case ONE:
                view = inflater.inflate(R.layout.home_recycler_item_one, parent, false);
                viewHolder = new ViewHolderOne(view);
                break;
            case TWO:
                view = inflater.inflate(R.layout.home_recycler_item_one, parent, false);
                viewHolder = new ViewHolderTwo(view);
                break;
            case THREE:
                view = inflater.inflate(R.layout.home_recycler_item_three, parent, false);
                viewHolder = new ViewHolderThree(view);
                break;
        }
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        switch (getItemViewType(position)) {
            case ONE://免费试听
                ViewHolderOne holderOne = (ViewHolderOne) holder;
                holderOne.home_title_text_left.setText("免费试听");
                HomeOneAdapter myHomeOneAdapter = new HomeOneAdapter(context, result.tryCourse);
                holderOne.home_recyclerView_one.setAdapter(myHomeOneAdapter);
                holderOne.home_recyclerView_one.addItemDecoration(new DividerItemDecoration(context, DividerItemDecoration.VERTICAL_LIST));
                holderOne.home_recyclerView_one.setLayoutManager(new LinearLayoutManager(context));
                holderOne.home_title_autoRelative.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
//                        homeFragmentCallback.jumpFragment("one");
                        Intent intent = new Intent(context, FreeAuditionActivity.class);
                        intent.putExtra("name", "免费试听");
                        context.startActivity(intent);
                    }
                });

                //监听 条目点击
                myHomeOneAdapter.setRecyclerViewItemOnClick(new RecyclerViewItemOnClick() {
                    @Override
                    public void onItemClick(int postion, String object_id) {
                        Intent intent = new Intent(context, PlayVideoActivity.class);
                        intent.putExtra("object_id", object_id);
                        context.startActivity(intent);
                    }
                });
                break;
            case TWO://精选课程
                ViewHolderTwo holderTwo = (ViewHolderTwo) holder;
                holderTwo.home_title_text_left.setText("精选课程");
                holderTwo.home_recyclerView_one.addItemDecoration(new DividerItemDecoration(context, DividerItemDecoration.VERTICAL_LIST));
                holderTwo.home_recyclerView_one.setLayoutManager(new LinearLayoutManager(context));
                HomeTwoAdapter myHomeTwoAdapter = new HomeTwoAdapter(context, result.qualityCourse);
                //监听 条目点击
                myHomeTwoAdapter.setRecyclerViewItemOnClick(new RecyclerViewItemOnClick() {
                    @Override
                    public void onItemClick(int postion, String object_id) {
                        Intent intent = new Intent(context, PlayVideoActivity.class);
                        intent.putExtra("object_id", object_id);
                        context.startActivity(intent);
                    }
                });
                holderTwo.home_recyclerView_one.setAdapter(myHomeTwoAdapter);


                holderTwo.home_title_autoRelative.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
//                        homeFragmentCallback.jumpFragment("two");
                        Intent intent = new Intent(context, FreeAuditionActivity.class);
                        intent.putExtra("name", "精选课程");
                        context.startActivity(intent);
                    }
                });
                break;
            case THREE://精选专辑
                ViewHolderThree holderThree = (ViewHolderThree) holder;
                holderThree.home_title_text_left.setText("精选专辑");

                HomeThreeAdapter homeThreeAdapter = new HomeThreeAdapter(context, result.topicCourse);
                holderThree.home_recyclerView_two.setLayoutManager(new GridLayoutManager(context, 3));
                holderThree.home_recyclerView_two.setAdapter(homeThreeAdapter);
                holderThree.home_title_autoRelative.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
//                        homeFragmentCallback.jumpFragment("three");
                        Intent intent = new Intent(context, GreatestHitsActivity.class);
                        intent.putExtra("name", "精选专辑");
                        context.startActivity(intent);
                    }
                });

                //监听 条目点击
                homeThreeAdapter.setRecyclerViewItemOnClick(new RecyclerViewItemOnClick() {
                    @Override
                    public void onItemClick(int postion, String object_id) {
                        Intent intent = new Intent(context, PlayVideoActivity.class);
                        intent.putExtra("object_id", object_id);
                        context.startActivity(intent);
                    }
                });
                break;
        }
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0)
            return ONE;
        else if (position == 1)
            return TWO;
        else if (position == 2)
            return THREE;
        else
            return ONE;
    }

    @Override
    public int getItemCount() {
        return ITEMCOUNT;
    }

    class ViewHolderOne extends RecyclerView.ViewHolder {

        private final RecyclerView home_recyclerView_one;
        private final TextView home_title_text_left;
        private final AutoRelativeLayout home_title_autoRelative;

        public ViewHolderOne(View itemView) {
            super(itemView);
            home_recyclerView_one = (RecyclerView) itemView.findViewById(R.id.home_recyclerView_one);
            home_title_text_left = (TextView) itemView.findViewById(R.id.home_title_text_left);
            home_title_autoRelative = (AutoRelativeLayout) itemView.findViewById(R.id.home_title_autoRelative);
        }
    }

    class ViewHolderTwo extends RecyclerView.ViewHolder {

        private final RecyclerView home_recyclerView_one;
        private final TextView home_title_text_left;
        private final AutoRelativeLayout home_title_autoRelative;

        public ViewHolderTwo(View itemView) {
            super(itemView);
            home_recyclerView_one = (RecyclerView) itemView.findViewById(R.id.home_recyclerView_one);
            home_title_text_left = (TextView) itemView.findViewById(R.id.home_title_text_left);
            home_title_autoRelative = (AutoRelativeLayout) itemView.findViewById(R.id.home_title_autoRelative);
        }
    }

    class ViewHolderThree extends RecyclerView.ViewHolder {

        private final RecyclerView home_recyclerView_two;
        private final TextView home_title_text_left;
        private final AutoRelativeLayout home_title_autoRelative;

        public ViewHolderThree(View itemView) {
            super(itemView);
            home_recyclerView_two = (RecyclerView) itemView.findViewById(R.id.home_recyclerView_two);
            home_title_text_left = (TextView) itemView.findViewById(R.id.home_title_text_left);
            home_title_autoRelative = (AutoRelativeLayout) itemView.findViewById(R.id.home_title_autoRelative);
        }
    }
}
